#include <iostream>
#include <algorithm>
#include <unordered_map>
using namespace std;
int t;
int n; 
const int N=1e5+10;
struct node{
	int x,y,e;
}a[N];
int disc[N*2];
int fa[N*2];
int find(int x){
	if(fa[x]==x)return x;
	return fa[x]=find(fa[x]);
}
void un(int x,int y)
{
	fa[find(x)]=find(y);
}
bool sasame(int x,int y)
{
	return find(x)==find(y);
}
int main()
{
	cin>>t;
	while(t--)
	{
		cin>>n;
		int pos=0;
		for(int i=1;i<=n;i++)
		{
			cin>>a[i].x>>a[i].y>>a[i].e;
			disc[++pos]=a[i].x;disc[++pos]=a[i].y;
		}
		sort(disc+1,disc+1+pos);
		int cnt=0;
		unordered_map<int,int> mp;
		for(int i=1;i<=pos;i++)
		{
			int x=disc[i];
			if(mp.count(x))continue;
			cnt++;
			mp[x]=cnt;
		}
		for(int i=1;i<=cnt;i++)fa[i]=i;
	
		for(int i=1;i<=n;i++)
		{
			int x=a[i].x,y=a[i].y,e=a[i].e;
			if(e==1)un(mp[x],mp[y]);
		}
		int count=0;
		for(int i=1;i<=n;i++)
		{
			int x=a[i].x,y=a[i].y,e=a[i].e;
			if(e==0)
			{
				if(sasame(mp[x],mp[y]))
				{
			   cout<<"No"<<endl;
				count=1;
				break;
			   }
			}
		}
		if(count==0)
		cout<<"Yes"<<endl;
		
	}
	return 0;
}
